package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.LocaleWorkQueryCountAdapter;
import com.isunland.managesystem.base.Base;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.LocaleWorkQueryCount;
import com.isunland.managesystem.entity.LocaleWorkQueryCountListOriginal;
import com.isunland.managesystem.entity.LocaleWorkQueryCountParams;
import com.isunland.managesystem.entity.LocaleWorkQueryCountQueryParams;
import com.isunland.managesystem.utils.MyDateUtil;
import com.isunland.managesystem.utils.ParamsNotEmpty;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * 对象功能: 作业任务列表Fragment
 * 开发公司: 北京信普达系统工程有限公司
 * 开发人员: wangyanan
 * 创建时间: 2016/12/8 16:29
 */

public class LocaleWorkQueryCountListFragment extends BaseListFragment {
  /** 是否修改成功, */
  public static final String EXTRA_IF_EDIT =
      LocaleWorkQueryCountListFragment.class.getSimpleName() + ".EXTRA_IF_EDIT";
  private static final int REQUEST_QUERY = 1;
  private static final int REQUEST_ADD = 3;
  private static final int REQUEST_DETAIL = 4;
  public static final int FROM_PLOTMAPPINGINFORMATION = 5;

  private LocaleWorkQueryCountAdapter<LocaleWorkQueryCount> mAdapter;
  private ArrayList<LocaleWorkQueryCount> mList;
  //private TextView mStartDateTv;
  //private TextView mEndDateTv;
  /** 查询参数 */
  private LocaleWorkQueryCountQueryParams mParamsQuery;
  private LocaleWorkQueryCountParams mParams;

  @Override public void initData() {
    super.initData();
    if (mBaseParams != null && mBaseParams instanceof LocaleWorkQueryCountParams) {
      mParams = (LocaleWorkQueryCountParams) mBaseParams;
    }
    if (mParams == null) mParams = new LocaleWorkQueryCountParams();

    mList = new ArrayList<>();
    mAdapter = new LocaleWorkQueryCountAdapter<>(mActivity, mList);
    mParamsQuery = new LocaleWorkQueryCountQueryParams();
    mParamsQuery.setEndDate(new Date());
    mParamsQuery.setStartDate(MyDateUtil.getBeforeMonth(1));
  }

  @Override public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    setTitleCustom(R.string.operationTask);
    setListAdapter(mAdapter);
  }

  @Override public HashMap<String, String> getparameters() {
    ParamsNotEmpty params = new ParamsNotEmpty();

    params.put("jobNo", CurrentUser.newInstance(mActivity).getJobNumber());
    //params.put("jobNo","1");
    //noinspection deprecation
    params.put("startDate",
        MyDateUtil.format(MyDateUtil.getBeginOfDay(mParamsQuery.getStartDate())));
    //noinspection deprecation
    params.put("endDate", MyDateUtil.format(MyDateUtil.getEndofDay(mParamsQuery.getEndDate())));
    params.put("devicecodeLike", mParamsQuery.getEquipment());
    params.put("areaid", mParams.getAreaId());
    params.put("joinStaffNames", mParamsQuery.getMan());
    params.put("memberCode", mParamsQuery.getOrgCode());

    return params.getParams();
  }

  @Override public String getUrl() {
    //测试url
    return ApiConst.URL_OPERATION_TASK_LIST;
  }

  @Override public void resolveResponse(String json) {
    //测试json数据.放在文件中.
    //json = MyUtils.readAssetsFile(MyApplication.getAppContext(), "test.json");
    LocaleWorkQueryCountListOriginal result =
        new Gson().fromJson(json, LocaleWorkQueryCountListOriginal.class);
    if (result.getResult() == Base.SUCCESS && result.getRows() != null) {
      //获取列表成功

      mList.clear();
      mList.addAll(result.getRows());
      mAdapter.notifyDataSetChanged();
    } else {
      ToastUtil.show("获取列表失败");
    }
  }

  //test
  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) return;
    if (requestCode == REQUEST_QUERY && data != null) {
      LocaleWorkQueryCountQueryParams params =
          (LocaleWorkQueryCountQueryParams) data.getSerializableExtra(BaseFragment.EXTRA_PARAMS);
      mParamsQuery = params == null ? new LocaleWorkQueryCountQueryParams() : params;
      volleyPost();
      return;
    }

    if (requestCode == REQUEST_ADD) {

      volleyPost();
      return;
    }
    if (requestCode == REQUEST_DETAIL && (data != null) && data.getBooleanExtra(EXTRA_IF_EDIT,
        false)) {
      volleyPost();
      return;
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

  @Override public void onListItemClick(ListView l, View v, int position, long id) {
    LocaleWorkQueryCount item = mAdapter.getItem(position - mListview.getHeaderViewsCount());

    LocaleWorkQueryCountPagerActivity.newInstance(this, mActivity, item, REQUEST_DETAIL);
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_add_search, menu);
    if (mParams.isForbiddenAdd()) {
      menu.findItem(R.id.menu_item_add).setVisible(false);
    }

    super.onCreateOptionsMenu(menu, inflater);
  }

  //test3
  @Override public boolean onOptionsItemSelected(MenuItem item) {
    if (item == null) return false;
    switch (item.getItemId()) {
      case android.R.id.home:
        getActivity().finish();
        return true;
      case R.id.menu_item_add:
        //ToastUtil.show("添加");

        startActivityForResult(new Intent(getActivity(), LocaleWorkQueryCountUpdateActivity.class),
            REQUEST_ADD);
        return true;

      case R.id.menu_item_querys:
        LocaleWorkQueryCountQueryActivity.newInstance(this, LocaleWorkQueryCountQueryActivity.class,
            mParamsQuery, REQUEST_QUERY);

        return true;
    }

    return super.onOptionsItemSelected(item);
  }
}
